<?php
/**
* @Auth By AutoCreateCode
* @Desc Withdraw类
* @Date 2016-03-08 09:58:34
*/
namespace App\Mod;
use Lib\Model;
class Withdraw extends Model{
    /**
    * 插入数据
    * @access public
    * @param Array $data 要插入的数据
    * @return Bool
    */
    public function addWithdraw($data){
        if(empty($data) || !is_array($data)){
            return false;
        }
        
        $validator = new \Lib\Validator($data);
        $validator->setRules([
            'business_id' => ['required'=>true,'validate'=>'absint'],
            'status' => ['required'=>'','validate'=>'absint'],
            'business_name' => ['required'=>true,'validate'=>'strip_tags,trim'],
            'wd_flow_no' => ['required'=>true,'validate'=>'strip_tags,trim'],
            'wd_money' => ['required'=>true,'validate'=>'strip_tags,trim'],
            'apply_time' => ['required'=>true,'validate'=>'absint'],
            'audit_time' => ['required'=>'','validate'=>'absint'],
            'transfer_way' => ['required'=>'','validate'=>'absint'],
            'transfer_no' => ['required'=>'','validate'=>'strip_tags,trim'],
            'transfer_time' => ['required'=>'','validate'=>'absint'],
            'operator' => ['required'=>'','validate'=>'strip_tags,trim'],
            'operator_ip' => ['required'=>'','validate'=>'strip_tags,trim'],
            'remark' => ['required'=>'','validate'=>'strip_tags,trim'],
        ])->setMessages([
            'business_id' => ['required'=>'商户id不能为空','validate'=>'商户id填写不正确'],
            'status' => ['required'=>'0待 1通过 2拒绝不能为空','validate'=>'0待 1通过 2拒绝填写不正确'],
            'business_name' => ['required'=>'商户名称不能为空','validate'=>'商户名称填写不正确'],
            'wd_flow_no' => ['required'=>'提现流水号不能为空','validate'=>'提现流水号填写不正确'],
            'wd_money' => ['required'=>'提现金额不能为空','validate'=>'提现金额填写不正确'],
            'apply_time' => ['required'=>'申请时间不能为空','validate'=>'申请时间填写不正确'],
            'audit_time' => ['required'=>'审核时间不能为空','validate'=>'审核时间填写不正确'],
            'transfer_way' => ['required'=>'转账方式不能为空','validate'=>'转账方式填写不正确'],
            'transfer_no' => ['required'=>'转账交易号不能为空','validate'=>'转账交易号填写不正确'],
            'transfer_time' => ['required'=>'转账时间不能为空','validate'=>'转账时间填写不正确'],
            'operator' => ['required'=>'操作人账号不能为空','validate'=>'操作人账号填写不正确'],
            'operator_ip' => ['required'=>'操作人IP不能为空','validate'=>'操作人IP填写不正确'],
            'remark' => ['required'=>'备注不能为空','validate'=>'备注填写不正确'],
        ]);
    
        $data = $validator->validate();
        if(!$data){
            $this->error = $validator->getError();
            return false;
        }
        
        return $this->insert($data);
    }

    /**
    * 根据ID删除多条记录
    * @access public
    * @param Mix $id 要删除的ID，可为数组，也可用逗号用隔
    * @return Bool
    */
    public function delWithdraw($id){
        if(empty($id) || (!is_numeric($id) && !is_array($id))){
            return false;
        }
        $ids = is_array($id) ? join(',',$id) : $id;
        return $this->where("id in({$ids})")->delete();
    }

    /**
    * 根据ID修改记录
    * @access public
    * @param Int $id 要修改的ID
    * @param Array $data 要修改的数据
    * @return Bool
    */
    public function updateWithdraw($id,$data){
        if(empty($id) || (!is_numeric($id) && !is_array($id)) || empty($data) || !is_array($data)){
            return false;
        }
        
        $validator = new \Lib\Validator($data);
        $validator->setRules([
            'business_id' => ['validate'=>'absint'],
            'status' => ['validate'=>'absint'],
            'business_name' => ['validate'=>'strip_tags,trim'],
            'wd_flow_no' => ['validate'=>'strip_tags,trim'],
            'wd_money' => ['validate'=>'strip_tags,trim'],
            'apply_time' => ['validate'=>'absint'],
            'audit_time' => ['validate'=>'absint'],
            'transfer_way' => ['validate'=>'absint'],
            'transfer_no' => ['validate'=>'strip_tags,trim'],
            'transfer_time' => ['validate'=>'absint'],
            'operator' => ['validate'=>'strip_tags,trim'],
            'operator_ip' => ['validate'=>'strip_tags,trim'],
            'remark' => ['validate'=>'strip_tags,trim'],
        ])->setMessages([
            'business_id' => ['validate'=>'商户id填写不正确'],
            'status' => ['validate'=>'0待 1通过 2拒绝填写不正确'],
            'business_name' => ['validate'=>'商户名称填写不正确'],
            'wd_flow_no' => ['validate'=>'提现流水号填写不正确'],
            'wd_money' => ['validate'=>'提现金额填写不正确'],
            'apply_time' => ['validate'=>'申请时间填写不正确'],
            'audit_time' => ['validate'=>'审核时间填写不正确'],
            'transfer_way' => ['validate'=>'转账方式填写不正确'],
            'transfer_no' => ['validate'=>'转账交易号填写不正确'],
            'transfer_time' => ['validate'=>'转账时间填写不正确'],
            'operator' => ['validate'=>'操作人账号填写不正确'],
            'operator_ip' => ['validate'=>'操作人IP填写不正确'],
            'remark' => ['validate'=>'备注填写不正确'],
        ]);
    
        $data = $validator->validate();
        if(!$data){
            $this->error = $validator->getError();
            return false;
        }
    
        $ids = is_array($id) ? join(',',$id) : $id;
        return $this->where("id in({$ids})")->update($data);
    }

    /**
    * 根据ID获取单条记录
    * @access public
    * @param Int $id 要查询的ID
    * @param String $fields 要返回的字段
    * @return Array
    */
    public function getWithdrawById($id,$fields="*"){
        if(empty($id) || !is_numeric($id)){
            return false;
        }
        return $this->where("id={$id}")->fields($fields)->fetch();
    }

    /**
    * 根据多个ID获取多条记录
    * @access public
    * @param Mix $ids 要查询的ID,可为数组，也可以用逗号分隔
    * @param String $fields 要返回的字段
    * @return Array
    */
    public function getWithdrawByIds($ids,$fields="*"){
        if(!is_array($ids)){
            $ids = explode(',',$ids);
        }
        $ids = join(',',$ids);
        return $this->where("id in($ids)")->order("find_in_set(id,'{$ids}')")->fields($fields)->select();
    }

    /**
    * 根据SQL条件查询记录
    * @access public
    * @param String $condition 查询条件
    * @param Int $offset 偏移量
    * @param Int $size 返回条数
    * @param String $order 排序
    * @param String $fields 要返回的字段
    * @return Mix Array
    */
    public function search($condition,$offset=0,$size=20,$fields='*',$order='id desc'){
        return $this->fields($fields)->where($condition)->order($order)->limit($offset,$size)->select();
    }

    /**
    * 根据条件返回记录数
    * @access public
    * @param String $condition 查询条件
    * @return Int
    */
    public function searchCount($condition){
        return $this->where($condition)->count();
    }
}